***Numeracy (only from 2002)***

*2002
use "${original}H02D_r.dta", clear
rename *, lower
keep hhid pn hd178 hd179 hd180

gen hhidpn = hhid+ pn

gen hd178_ok = 1 if hd178==100
replace hd178_ok = 0 if (!missing(hd178) & hd178!=100)
rename hd178_ok r6num1_disease
label variable r6num1_disease "r6num1: disease"

gen hd179_ok = 1 if hd179==400000
replace hd179_ok = 0 if (!missing(hd179) & hd179!=400000)
rename hd179_ok r6num2_lottery
label variable r6num2_lottery "r6num2: lottery"

gen hd180_ok = 1 if hd180==242
replace hd180_ok = 0 if (!missing(hd180) & hd180!=242)
rename hd180_ok r6num3_interest
label variable r6num3_interest "r6num3: interest"

save H02D_r_new.dta, replace

*2004
use "${original}H04D_r.dta", clear
rename *, lower

keep hhid pn jd178 jd179 jd180

gen hhidpn = hhid+ pn

gen jd178_ok = 1 if jd178==100
replace jd178_ok = 0 if (!missing(jd178) & jd178!=100)
rename jd178_ok r7num1_disease
label variable r7num1_disease "r7num1: disease"

gen jd179_ok = 1 if jd179==400000
replace jd179_ok = 0 if (!missing(jd179) & jd179!=400000)
rename jd179_ok r7num2_lottery
label variable r7num2_lottery "r7num2: lottery"

gen jd180_ok = 1 if jd180==242
replace jd180_ok = 0 if (!missing(jd180) & jd180!=242)
rename jd180_ok r7num3_interest
label variable r7num3_interest "r7num3: interest"

save H04D_r_new.dta, replace

*2006
use "${original}H06D_r.dta", clear
rename *, lower

keep hhid pn kd178 kd179 kd180

gen hhidpn = hhid+ pn

gen kd178_ok = 1 if kd178==100
replace kd178_ok = 0 if (!missing(kd178) & kd178!=100)
rename kd178_ok r8num1_disease
label variable r8num1_disease "r8num1: disease"

gen kd179_ok = 1 if kd179==400000
replace kd179_ok = 0 if (!missing(kd179) & kd179!=400000)
rename kd179_ok r8num2_lottery
label variable r8num2_lottery "r8num2: lottery"

gen kd180_ok = 1 if kd180==242
replace kd180_ok = 0 if (!missing(kd180) & kd180!=242)
rename kd180_ok r8num3_interest
label variable r8num3_interest "r8num3: interest"

save H06D_r_new.dta, replace

*2008
use "${original}H08D_r.dta", clear
rename *, lower

keep hhid pn ld178 ld179 ld180

gen hhidpn = hhid+ pn

gen ld178_ok = 1 if ld178==100
replace ld178_ok = 0 if (!missing(ld178) & ld178!=100)
rename ld178_ok r9num1_disease
label variable r9num1_disease "r9num1: disease"

gen ld179_ok = 1 if ld179==400000
replace ld179_ok = 0 if (!missing(ld179) & ld179!=400000)
rename ld179_ok r9num2_lottery
label variable r9num2_lottery "r9num2: lottery"

gen ld180_ok = 1 if ld180==242
replace ld180_ok = 0 if (!missing(ld180) & ld180!=242)
rename ld180_ok r9num3_interest
label variable r9num3_interest "r9num3: interest"

save H08D_r_new.dta, replace

*2010
use "${original}H10D_r.dta", clear
rename *, lower

keep hhid pn md178 md179 md180

gen hhidpn = hhid+ pn

gen md178_ok = 1 if md178==100
replace md178_ok = 0 if (!missing(md178) & md178!=100)
rename md178_ok r10num1_disease
label variable r10num1_disease "r10num1: disease"

gen md179_ok = 1 if md179==400000
replace md179_ok = 0 if (!missing(md179) & md179!=400000)
rename md179_ok r10num2_lottery
label variable r10num2_lottery "r10num2: lottery"

gen md180_ok = 1 if md180==242
replace md180_ok = 0 if (!missing(md180) & md180!=242)
rename md180_ok r10num3_interest
label variable r10num3_interest "r10num3: interest"

save H10D_r_new.dta, replace

*2012
use "${original}H12D_r.dta", clear
rename *, lower

keep hhid pn nd178 nd179 nd180

gen hhidpn = hhid+ pn

gen nd178_ok = 1 if nd178==100
replace nd178_ok = 0 if (!missing(nd178) & nd178!=100)
rename nd178_ok r11num1_disease
label variable r11num1_disease "r11num1: disease"

gen nd179_ok = 1 if nd179==400000
replace nd179_ok = 0 if (!missing(nd179) & nd179!=400000)
rename nd179_ok r11num2_lottery
label variable r11num2_lottery "r11num2: lottery"

gen nd180_ok = 1 if nd180==242
replace nd180_ok = 0 if (!missing(nd180) & nd180!=242)
rename nd180_ok r11num3_interest
label variable r11num3_interest "r11num3: interest"

save H12D_r_new.dta, replace

*2014
use "${original}H14D_r.dta", clear
rename *, lower

keep hhid pn od178 od179 od180

gen hhidpn = hhid+ pn

gen od178_ok = 1 if od178==100
replace od178_ok = 0 if (!missing(od178) & od178!=100)
rename od178_ok r12num1_disease
label variable r12num1_disease "r12num1: disease"

gen od179_ok = 1 if od179==400000
replace od179_ok = 0 if (!missing(od179) & od179!=400000)
rename od179_ok r12num2_lottery
label variable r12num2_lottery "r12num2: lottery"

gen od180_ok = 1 if od180==242
replace od180_ok = 0 if (!missing(od180) & od180!=242)
rename od180_ok r12num3_interest
label variable r12num3_interest "r12num3: interest"

save H14D_r_new.dta, replace


*Merge
use H02D_r_new.dta, clear
keep hhid pn r6num1_disease r6num2_lottery r6num3_interest
merge 1:1 hhid pn using H04D_r_new.dta, keepusing(r7num1_disease r7num2_lottery r7num3_interest)
drop _m
merge 1:1 hhid pn using H06D_r_new.dta, keepusing(r8num1_disease r8num2_lottery r8num3_interest)
drop _m
merge 1:1 hhid pn using H08D_r_new.dta, keepusing(r9num1_disease r9num2_lottery r9num3_interest)
drop _m
merge 1:1 hhid pn using H10D_r_new.dta, keepusing(r10num1_disease r10num2_lottery r10num3_interest)
drop _m
merge 1:1 hhid pn using H12D_r_new.dta, keepusing(r11num1_disease r11num2_lottery r11num3_interest)
drop _m
merge 1:1 hhid pn using H14D_r_new.dta, keepusing(r12num1_disease r12num2_lottery r12num3_interest)
drop _m

save numeracy.dta, replace

*Erase temporary datasets
foreach n in 02 04 06 08 10 12 14 {
erase H`n'D_r_new.dta
}